const qs = require("qs");
import Logger from "./logger";
const getCurrentInstance = () => {
    const pages = getCurrentPages();
    return pages[pages.length - 1];
}
const transOption = function (opt, after = (to, from) => {}) {
    let url = opt.path;
    let q = opt.query || {};
    if (url.indexOf('?') !== -1) {
        url += "&" + qs.stringify(q);
    } else if (q && Object.keys(q).length) {
        url += "?" + qs.stringify(q);
    }
    let ret = {
        ...opt,
        url,
        success: (r) => {
            typeof opt.success === "function" && opt.success(r);
            const current = getCurrentInstance();
            typeof after === "function" && after({
                url,
                options: q
            }, {
                url: current.route,
                options: current.options
            });
        },
    }
    return ret;
}
const defaultOpt = {
    logger: false,
    middleware: []
}
class Router {
    tabbars = [];
    middleware = [];
    logger = false;
    $option = {
        ...defaultOpt
    }
    constructor(tabbars = [], opt = defaultOpt) {
        this.tabbars = tabbars;
        this.$option = Object.assign(opt);
        if (opt.logger) {
            this.middleware.push(Logger);
        }
    }
    beforeEach(to, next) {
        typeof next === "function" && next();
    }
    afterEach(to, from) {
        this.middle(to, from);
    }
    middle(to, from) {
        return this.middleware.map(fn => {
            if (typeof fn === "function") {
                fn(to, from, this);
            }
        })
    }
    go(n = -1) {
        return this.back(n);
    }
    back(n = -1) {
        const pages = getCurrentPages();
        const current = getCurrentInstance();
        if (Math.abs(n) > pages.length - 1) {
            n = -pages.length + 1;
        }
        const backPage = pages[pages.length - n] || {
            route: "/",
            options: {},
        };
        this.afterEach({
            url: backPage.route,
            options: backPage.options
        }, {
            url: current.route,
            options: current.options
        })
        uni.navigateBack({
            delta: n
        });
    }
    switchTab(opt) {
        if (typeof opt === "string") {
            opt = {
                path: opt
            };
        }
        let o = transOption(opt, this.afterEach.bind(this));
        this.beforeEach(o, () => {
            uni.switchTab(o);
        });
    }
    push(opt) {
        if (typeof opt === "string") {
            opt = {
                path: opt
            };
        }
        let o = transOption(opt, this.afterEach.bind(this));
        if (this.tabbars.length) {
            let path = opt.path;
            path = path.substr(0, path.indexOf("?"))
            if (this.tabbars.indexOf(path) != -1) {
                return this.reLaunch(opt);
            }
        }
        this.beforeEach(o, () => {
            uni.navigateTo(o);
        });
    }
    reLaunch(opt) {
        if (typeof opt === "string") {
            opt = {
                path: opt
            };
        }
        let o = transOption(opt, this.afterEach);

        this.beforeEach(o, () => {
            uni.reLaunch(o);
        });
    }
}
let router = new Router(["/pages/tabbar-page/tabbar-page"], {
    logger: true
});
export default router;